﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Web.Common;
using CollegeWebsite.Areas.Administrator.Models;

namespace CollegeWebsite.Areas.Administrator.Controllers
{

    public class AccountController : Controller
    {

        // *************** LOGIC METHODS **************
        public bool IsLogin(Admin admin)
        {
            if (Session["AdminAccount"] != null) return true;

            var entity = new CollegeWebsiteDataContext();

            var result = entity.Admins.Select(a => a).FirstOrDefault(a => (a.UserName == admin.UserName && Sercure.VerifyMd5Hash(admin.PassWord, a.PassWord)));
            if (result != null)
            {
                Session.Add("AdminAccount", admin);
                return true;
            }
            else
            {
                return false;
            }
        }




        //************** ACTION RESULT ****************

        //
        // GET: /Administrator/Administrator/
        [DisplayName("Logon")]
        public ActionResult Index()
        {
            return View("AdminLogOn");
        }
        // Load LogOn View

        [DisplayName("Admin Logon")]
        public ActionResult AdminLogOn()
        {
            return View();
        }

        /// <summary>
        /// Get LogOn form data, Check Login, then return back to page required security role
        /// </summary>
        [HttpPost]
        public ActionResult AdminLogOn(Admin admin, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (IsLogin(admin))
                {
                    if(!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    return RedirectToAction("Index", "Home");
                }
              }
            return View();  
        }

        /// <summary>
        /// Load Create Admin Account Form;
        /// </summary>
        /// <returns>Action Result -> Load Page View</returns>
        /// <remarks></remarks>
        [DisplayName("Admin Login Via Ajax")]
        public ActionResult AddAdminAccount(bool? ajax)
        {
           //CollegeWebsite.Web.Common.Ajax.

            return Ajax.IsAjax(View(), ajax ?? true);
        }

        /// <summary>
        /// Get new new Admin Account Infomation 
        /// </summary>
        /// <param name="adm"></param>
         /// <param name="ajax"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        [HttpPost]
        public ActionResult AddAdminAccount(Admin adm, bool? ajax)
        {
            if (ajax == true)
            {
                return View("AddAdminAccount");
            }
            return View("AddAdminAccount");
        }
    }



}
